package com.jpl.demo.mobility.ui;

import com.vaadin.service.ApplicationContext;
import com.vaadin.terminal.gwt.server.WebApplicationContext;
import com.vaadin.terminal.gwt.server.WebBrowser;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.VerticalLayout;

public class BrowserInfoExample extends CustomComponent {
	private static final long serialVersionUID = 97529549237L;

	String context;
	VerticalLayout layout = new VerticalLayout();

	public void init(String context) {
		this.context = context;
		setCompositionRoot(layout);
		basic();
	}

	void basic() {
		// BEGIN-EXAMPLE: advanced.browserinfo.basic
		// Get the browser info object

		ApplicationContext context = getWindow().getApplication().getContext();
		WebBrowser browser = ((WebApplicationContext) context).getBrowser();
		FormLayout items = new FormLayout();

		// Detect browser type
		String browserName = "unknown";
		if (browser.isOpera())
			browserName = "Opera";
		else if (browser.isSafari())
			browserName = "Apple Safari";
		else if (browser.isChrome())
			browserName = "Google Chrome";
		else if (browser.isIE())
			browserName = "Internet Explorer";
		else if (browser.isFirefox())
			browserName = "Mozilla Firefox";
		Label browserField = new Label(browserName);
		browserField.setCaption("Your browser:");
		items.addComponent(browserField);

		Label major = new Label(
				String.valueOf(browser.getBrowserMajorVersion()));
		major.setCaption("Major Version:");
		items.addComponent(major);

		Label minor = new Label(
				String.valueOf(browser.getBrowserMinorVersion()));
		minor.setCaption("Minor Version:");
		items.addComponent(minor);

		// Detect operating system
		String osName = "unknown";
		if (browser.isLinux())
			osName = "Linux";
		else if (browser.isWindows())
			osName = "Windows";
		else if (browser.isMacOSX())
			osName = "Mac OS X";
		Label os = new Label(osName);
		os.setCaption("Operating System:");
		items.addComponent(os);

		// Screen size
		Label screenSize = new Label(browser.getScreenWidth() + "×"
				+ browser.getScreenHeight());
		screenSize.setCaption("Screen Size:");
		items.addComponent(screenSize);

		// IP address
		Label ip = new Label(browser.getAddress());
		ip.setCaption("IP Address:");
		items.addComponent(ip);

		// Get raw user-agent info string
		Label userAgent = new Label(browser.getBrowserApplication());
		userAgent.setCaption("User-Agent:");
		items.addComponent(userAgent);
		// END-EXAMPLE: advanced.browserinfo.basic

		layout.addComponent(items);
		layout.setSpacing(false);

		
	}
}